function [models_struct, is_nan] = create_models_struct( input, list_of_regions)

% This function  build a model's struct, that is made from an output from a classifier's run with 
% trained data. Each classifier need to have a matrix of data's features, and an array of labels that 
% are supposed to be attached to each data's features.

% Function Inputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% input - mat of num_featuresXnum_regions.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% list_of_regions - suggested label for each region, by the ground truth.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Function Outputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%  models_struct - struct of models that are the output from a classifier.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% is_nan - 1 if model is not valid (NaN), 0 otherwise
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Global Inputs:
% model_p.class_func
% model_p.class_kn
% model_p.class_adaboost_Maxitr
% model_p.class_adaboost_weak

global model_p
class_func=model_p.class_func;
class_kn=model_p.class_kn;
class_adaboost_Maxitr=model_p.class_adaboost_Maxitr;
class_adaboost_weak=model_p.class_adaboost_weak;

list0=find(list_of_regions==0);
input(:,list0)=[];
list_of_regions(list0)=[];
[list_of_regions_1,list_of_regions_2,list_of_regions_3,list_of_regions_4] =deal( list_of_regions);
is_nan=0;

if strcmp(class_func,'FLD')
    % renaming labels - FLD classifier works with the labels 1 & 2 only
    list_of_regions_1(list_of_regions_1~=1)=2;  % 1 is 1, 2 is others
    indices=find(list_of_regions_2==2);
    list_of_regions_2(:)=2;
    list_of_regions_2(indices)=1;                                   % 1 is 2, 2 is others
    indices=find(list_of_regions_3==3);
    list_of_regions_3(:)=2;
    list_of_regions_3(indices)=1;                                   % 1 is 3, 2 is others
    indices=find(list_of_regions_4==4);
    list_of_regions_4(:)=2;
    list_of_regions_4(indices)=1;                                   % 1 is 4, 2 is others
    
    %running classifier model's builder for each model
    if length(list_of_regions)>0 
        models_struct.model_1 = fld_train(struct('X',input,'y',list_of_regions_1));    
        models_struct.model_2 = fld_train(struct('X',input,'y',list_of_regions_2));    
        models_struct.model_3 = fld_train(struct('X',input,'y',list_of_regions_3));    
        models_struct.model_4 = fld_train(struct('X',input,'y',list_of_regions_4));    
        if (isnan(models_struct.model_1.b) | isnan(models_struct.model_2.b) | isnan(models_struct.model_3.b) |isnan(models_struct.model_4.b))
             is_nan=1;
        end        
    else
        [models_struct.model_1,models_struct.model_2,models_struct.model_3,models_struct.model_4]=deal(0);
    end
end 

if strcmp(class_func,'KNN')
    models_data_struct=struct('X',input,'y',list_of_regions);
    models_struct=knn_train(models_data_struct,class_kn);
end

if (strcmp(class_func,'RealAdaBoost') || strcmp(class_func,'ModestAdaBoost'))

    % renaming labels - classifier works with the labels 1 & 2 only
    list_of_regions_1(list_of_regions_1~=1)=-1;  % 1 is 1, -1 is others
    list_of_regions_2(list_of_regions_2~=2)=-1;
    list_of_regions_2(list_of_regions_2==2)=1;    % 1 is 2, -1 is others
    list_of_regions_3(list_of_regions_3~=3)=-1;
    list_of_regions_3(list_of_regions_3==3)=1;    % 1 is 3, -1 is others
    list_of_regions_4(list_of_regions_4~=4)=-1;
    list_of_regions_4(list_of_regions_4==4)=1;    % 1 is 4, -1 is others
    
    %running classifier model's builder for each model
    models_struct.weak_learner=tree_node_w(class_adaboost_weak);
    try
        if (strcmp(class_func,'RealAdaBoost'))
            [models_struct.model_1.Learners, models_struct.model_1.Weights] = real_adaboost_train(models_struct.weak_learner, input, list_of_regions_1, class_adaboost_Maxitr);
            [models_struct.model_2.Learners, models_struct.model_2.Weights] = real_adaboost_train(models_struct.weak_learner, input, list_of_regions_2, class_adaboost_Maxitr);
            [models_struct.model_3.Learners, models_struct.model_3.Weights] = real_adaboost_train(models_struct.weak_learner, input, list_of_regions_3, class_adaboost_Maxitr);
            [models_struct.model_4.Learners, models_struct.model_4.Weights] = real_adaboost_train(models_struct.weak_learner, input, list_of_regions_4, class_adaboost_Maxitr);
        end

        if (strcmp(class_func,'ModestAdaBoost'))
            [models_struct.model_1.Learners, models_struct.model_1.Weights] = modest_adaboost_train(models_struct.weak_learner, input, list_of_regions_1, class_adaboost_Maxitr);
            [models_struct.model_2.Learners, models_struct.model_2.Weights] = modest_adaboost_train(models_struct.weak_learner, input, list_of_regions_2, class_adaboost_Maxitr);
            [models_struct.model_3.Learners, models_struct.model_3.Weights] = modest_adaboost_train(models_struct.weak_learner, input, list_of_regions_3, class_adaboost_Maxitr);
            [models_struct.model_4.Learners, models_struct.model_4.Weights] = modest_adaboost_train(models_struct.weak_learner, input, list_of_regions_4, class_adaboost_Maxitr);
        end
    catch
        [models_struct.model_1,models_struct.model_2,models_struct.model_3,models_struct.model_4]=deal(0);
        is_nan=1;
    end
end